﻿// <copyright file="ModDependency.cs" company="SingularityShift">
// Copyright (c) 2009 SingularityShift.com
// </copyright>
// <author>Ben McClure</author>
// <email>ben@singularityshift.com</email>
// <date>2009-08-14</date>
// <summary>The primary implementation of an IModDependency, extending AbstractModMatcher.</summary>
namespace GameLib.Games.Components
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Text.RegularExpressions;

    /// <summary>
    /// <para>An AbstractModMatcher which implements the IModDependency interface.</para>
    /// <para>This represents one or more dependencies that an IMod requires.</para>
    /// </summary>
    public class ModDependency : AbstractModMatcher, IModDependency
    {
        /// <summary>
        /// Initializes a new instance of the ModDependency class, specifying owner mod, path, file pattern, and a message
        /// </summary>
        /// <param name="owner">The IMod which owns this dependency</param>
        /// <param name="path">The path to the dependency file</param>
        /// <param name="filePattern">The file or file pattern specifying the dependency</param>
        /// <param name="message">A message about this dependency</param>
        public ModDependency(IMod owner, string path, string filePattern, string message)
        {
            Owner = owner;
            FilePath = path;
            FilePattern = filePattern;
            Message = message;
        }

        /// <summary>
        /// Initializes a new instance of the ModDependency class, specifying an owner mod and file pattern
        /// </summary>
        /// <param name="owner">The IMod which owns this dependency</param>
        /// <param name="filePattern">The file or file pattern specifying the dependency</param>
        public ModDependency(IMod owner, string filePattern)
            : this(owner, null, filePattern, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModDependency class, specifying an owner mod, path, and file pattern
        /// </summary>
        /// <param name="owner">The IMod which owns this dependency</param>
        /// <param name="path">The path to the dependency file</param>
        /// <param name="filePattern">The file or file pattern specifying the dependency</param>
        public ModDependency(IMod owner, string path, string filePattern)
            : this(owner, path, filePattern, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModDependency class, specifying only the owner mod
        /// </summary>
        /// <param name="owner">The IMod which owns this dependency</param>
        public ModDependency(IMod owner)
        {
            Owner = owner;
        }

        /// <summary>
        /// Initializes a new instance of the ModDependency class for cloning
        /// </summary>
        public ModDependency()
        {
        }

        /// <summary>
        /// Creates a deep copy of this dependency
        /// </summary>
        /// <returns>A new ModDependency object which is equal but not the same object</returns>
        public override object Clone()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            ms.Flush();
            ms.Position = 0;
            return (IModDependency) bf.Deserialize(ms);
        }
    }
}
